﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyGiaoVuDAL.DTO;
using System.Data.SqlClient;
using QuanLyGiaoVuDAL.ADOWrapper;
using System.Data;
using QuanLyGiaoVuDAL.Mapper;

namespace QuanLyGiaoVuDAL.Repositories.DatabaseRepositories
{
    public class BauChonDeTaiRepository:IBauChonDeTaiRepository
    {
        /*InsertOrRemove>0 is Insert and <0 Remove*/
        public void InsertDeTaiBauChon(Guid SinhVienId, Guid MaDeTai)
        {
            SqlParameter[] parameters = new SqlParameter[3];
            parameters[0] = DBParameterFactory.Create("SinhVienId", SinhVienId);
            parameters[1] = DBParameterFactory.Create("MaDeTai", MaDeTai);
            parameters[2] = DBParameterFactory.Create("InserOrRemove", 1);
            try
            {
                var dbResult = StoreProcedureExecutor.ExecuteNonQueryStoredProcedure("[dbo].[usp_BauChonDeTai_Like_UnLike]", parameters);
            }
            catch (Exception ex)
            {
                return;
            }
            return;
        }
        public void RemoveDeTaiBauChon(Guid SinhVienId, Guid MaDeTai)
        {
            SqlParameter[] parameters = new SqlParameter[3];
            parameters[0] = DBParameterFactory.Create("SinhVienId", SinhVienId);
            parameters[1] = DBParameterFactory.Create("MaDeTai", MaDeTai);
            parameters[2] = DBParameterFactory.Create("InserOrRemove", -1);
            try
            {
                var dbResult = StoreProcedureExecutor.ExecuteNonQueryStoredProcedure("[dbo].[usp_BauChonDeTai_Like_UnLike]", parameters);
            }
            catch (Exception ex)
            {
                return;
            }
            return;
        }
        public IEnumerable<BauChonDeTaiDTO> GetAllDeTaiBauChon()
        {
            List<BauChonDeTaiDTO> deTaiList = new List<BauChonDeTaiDTO>();           
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_BauChonDeTaiSelect]");
            try
            {
                var caTable = dbResult.Tables[0].Rows;
                foreach (DataRow dataRow in caTable)
                {
                    deTaiList.Add(dataRow.ToBauChonDeTaiDTO());
                }
                return deTaiList.AsEnumerable();
            }
            catch (Exception ex)
            {
                return null;
            }               
        }
        public IEnumerable<BauChonDeTaiDTO> GetAllDeTaiSinhVienBauChon(Guid SinhVienId)
        {
            List<BauChonDeTaiDTO> deTaiList = new List<BauChonDeTaiDTO>();
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = DBParameterFactory.Create("SinhVienId", SinhVienId);
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_DanhSachDeTai_SinhVien_BauChon]",parameters);
            try
            {
                var caTable = dbResult.Tables[0].Rows;
                foreach (DataRow dataRow in caTable)
                {
                    deTaiList.Add(dataRow.ToBauChonDeTaiDTO());
                }
                return deTaiList.AsEnumerable();
            }
            catch (Exception ex)
            {
                return null;
            }               
        }
    }
}
